class Mockolo < Formula
  desc "Efficient Mock Generator for Swift"
  homepage "https://github.com/uber/mockolo"
  url "https://github.com/uber/mockolo/archive/refs/tags/2.4.0.tar.gz"
  sha256 "58174327732b53f9e6bb0c8c355270ae2df7be739f006680fd8eeb53599b6766"
  license "Apache-2.0"

  bottle do
    rebuild 1
    sha256 cellar: :any_skip_relocation, arm64_sequoia: "18f1b69b192e7aaf2cb57a752c4c9a96c2ea7ab24ce52dce3b138dac2b190599"
    sha256 cellar: :any_skip_relocation, arm64_sonoma:  "9da1cd6bc134cfd133dc72c56a3120429b8db32a4efab512f113c76f642f58de"
    sha256 cellar: :any_skip_relocation, sonoma:        "293816ce556079215b9f76165f1d306b2ece74018b4616411cd6086ebc99e291"
    sha256 cellar: :any_skip_relocation, arm64_linux:   "70236713bc4f9f9ede9f42093757fdf3df9b1852e228af98aa614bd463930745"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "476cee56c533c781c86fc767215cfc7ec7515596ef89f6ae473c3aff2927f1cd"
  end

  depends_on xcode: ["15.3", :build]

  uses_from_macos "swift" => :build

  def install
    args = if OS.mac?
      ["--disable-sandbox"]
    else
      ["--static-swift-stdlib"]
    end
    system "swift", "build", *args, "-c", "release", "--product", "mockolo"
    bin.install ".build/release/mockolo"
    generate_completions_from_executable(bin/"mockolo", "--generate-completion-script")
  end

  test do
    (testpath/"testfile.swift").write <<~SWIFT
      /// @mockable
      public protocol Foo {
          var num: Int { get set }
          func bar(arg: Float) -> String
      }
    SWIFT
    system bin/"mockolo", "-srcs", testpath/"testfile.swift", "-d", testpath/"GeneratedMocks.swift"
    assert_path_exists testpath/"GeneratedMocks.swift"
    output = <<~SWIFT.gsub(/\s+/, "").strip
      ///
      /// @Generated by Mockolo
      ///
      public class FooMock: Foo {
        public init() { }
        public init(num: Int = 0) {
            self.num = num
        }

        public private(set) var numSetCallCount = 0
        public var num: Int = 0 { didSet { numSetCallCount += 1 } }

        public private(set) var barCallCount = 0
        public var barHandler: ((Float) -> String)?
        public func bar(arg: Float) -> String {
            barCallCount += 1
            if let barHandler = barHandler {
                return barHandler(arg)
            }
            return ""
        }
      }
    SWIFT
    assert_equal output, shell_output("cat #{testpath/"GeneratedMocks.swift"}").gsub(/\s+/, "").strip
  end
end
